﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SellerProject.testfolder
{
    public partial class confirm_Y_continue_ICallBack : System.Web.UI.Page,ICallbackEventHandler
    {
        private string result;
        private Dictionary<string, Func<string>> dic;

        protected void Page_Load(object sender, EventArgs e)
        {
            //string reference = ClientScript.GetCallbackEventReference(this, "args", "Success", "", "Error", false);
            string reference = ClientScript.GetCallbackEventReference(this, "args", "Success", "", "Error", true);
            string callbackScript = "function CallServerMethod(args,context) {\n" + reference + ";\n}";

            //注册js脚本代码
            ClientScript.RegisterClientScriptBlock(this.GetType(), "CallServerMethod", callbackScript, true);
        }

        protected void btn_Click(object sender, EventArgs e)
        {
            //先执行一段代码
            Literal lit = form1.FindControl("lit") as Literal;
            lit.Text = DateTime.Now.ToString("yyyy-MM-dd");

            //弹出confirm
            ClientScript.RegisterStartupScript(GetType(), "confirm", "CallServerMethod(confirm('确认执行么？'),null);", true);
        }

        private string Confirm()
        {
            return "Confirm";
        }
        private string Cancel()
        {
            return "Cancel";
        }


        #region ICallbackEventHandler Members
        public string GetCallbackResult()
        {
            return result;
            //throw new NotImplementedException();
        }
        public void RaiseCallbackEvent(string eventArgument)
        {
            dic = new Dictionary<string, Func<string>>();
            dic.Add("true", Confirm);
            dic.Add("false", Cancel);

            result = dic[eventArgument]();
            //throw new NotImplementedException();
        }
        #endregion
    }
}